package com.dkm.orproject.dkmoa.service.serviceimpl.jurisdiction;


import com.dkm.orproject.dkmoa.dao.OaJurisdictionRoleMapper;
import com.dkm.orproject.dkmoa.dao.OaRoleMapper;
import com.dkm.orproject.dkmoa.dao.OaRolePersonnelMapper;
import com.dkm.orproject.dkmoa.domain.OaJurisdictionRole;
import com.dkm.orproject.dkmoa.domain.OaRole;
import com.dkm.orproject.dkmoa.domain.OaRolePersonnel;
import com.dkm.orproject.dkmoa.service.jurisdiction.OaRoleService;
import com.dkm.orproject.utils.utils.IdToobtainUtils;
import com.dkm.orproject.utils.utils.Tools;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @ClassName : OaRoleServiceImpl  //类名
 * @Description : 角色服务接口实现类  //描述
 * @Author : 易嵘  //作者
 * @Date: 2020-08-13 14:45  //时间
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class OaRoleServiceImpl implements OaRoleService {

    /**
     * 角色服务接口
     */
    @Resource
    OaRoleMapper roleMapper;
    @Resource
    OaJurisdictionRoleMapper jurisdictionRoleMapper;
    @Resource
    OaRolePersonnelMapper rolePersonnelMapper;
    @Resource
    Tools tools;
    @Resource
    IdToobtainUtils utils;

    @Override
    public int delete(Long pkRoleId) {
        return roleMapper.deleteByPrimaryKey(pkRoleId);
    }

    @Override
    public int insert(OaRole record,Long[] ids){
        if(ids!=null && ids.length>0){
            insertJurisdictionRole(record,ids);
        }
        return roleMapper.insertSelective(record);
    }


    @Override
    public OaRole selectBypkRoleId(Long pkRoleId) {
        return roleMapper.selectByPrimaryKey(pkRoleId);
    }

    @Override
    public int update(OaRole record,Long[] ids) {
        int j=jurisdictionRoleMapper.deleteById(record.getPkRoleId());
        int delete = rolePersonnelMapper.deleteByRoleId(record.getPkRoleId());
        if(ids!=null && ids.length>0){
            insertJurisdictionRole(record,ids);
        }
        return roleMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateById(Long pkRoleId, String updatedBy, String updatedTime) {
        return roleMapper.updateById(pkRoleId,updatedBy,updatedTime);
    }

    @Override
    public int deleteAll(List<Long> list) {
        return roleMapper.deleteAll(list);
    }

    @Override
    public int updateAll(List<Long> list, String updatedBy, String date) {
        return roleMapper.updateAll(list,updatedBy,date);
    }

    @Override
    public List<OaRole> findByAll() {
        return roleMapper.findByAll();
    }

    @Override
    public List<OaRole> findAll(OaRole role, Integer page, Integer limit, String startTime, String endTime) {
        PageHelper.startPage(page,limit);
        return roleMapper.findAll(role, startTime, endTime);
    }

    @Override
    public Long rowcount(OaRole role, String startTime, String endTime) {
        return roleMapper.rowcount(role,startTime,endTime);
    }

    @Override
    public OaRolePersonnel findByIds(Long personnelId) {
        return roleMapper.findByIds(personnelId);
    }

    /**
     * 增加角色与权限
     * @param record 角色
     * @param ids 权限
     */
    private  void insertJurisdictionRole(OaRole record,Long[] ids){
        List<Long> list= Arrays.asList(ids);
        List<OaJurisdictionRole> jurisdictionRoles=new ArrayList<>();
        for (Long aLong : list) {
            OaJurisdictionRole jurisdictionRole=new OaJurisdictionRole();
            jurisdictionRole.setPkJurisdictionRoleId(utils.getNumberId());
            jurisdictionRole.setIsRevision("1");
            jurisdictionRole.setRoleId(record.getPkRoleId());
            jurisdictionRole.setJurisdictionId(aLong);
            jurisdictionRoles.add(jurisdictionRole);
        }
        if(record.getRoleSuper()!=null){
            OaRolePersonnel rolePersonnel=new OaRolePersonnel();
            rolePersonnel.setPkRolePersonnelId(utils.getNumberId());
            rolePersonnel.setRoleId(record.getPkRoleId());
            rolePersonnel.setPersonnelId(record.getRoleSuper());
            rolePersonnel.setIsRevision("1");
            rolePersonnel.setCreatedBy(record.getCreatedBy());
            rolePersonnel.setCreatedTime(new Date());
            rolePersonnelMapper.insertSelective(rolePersonnel);
        }
        jurisdictionRoleMapper.insertAll(jurisdictionRoles);
    }
}
